<?php
##**********************************************************************************************
## upload hinh anh len web

function db_upload_photo($_FILES)
{		
	
		$date = date('Y-m-d H:i:s');
		
		 global $wpdb;
		 $overrides = array('test_form' => false);
		 $wp_upload_dir = wp_upload_dir();
		 $subdir = substr($wp_upload_dir['subdir'], 1);		 
		 $file = wp_handle_upload($_FILES, $overrides);
		 $filename		= $subdir.'/'.$_FILES['name'];
		
		 $data = array(
		 	'title'		=> '',
		 	'url'		=> $filename,
		 	'parent'	=> 0,
		 	'type'		=> 0,
		 	'date'		=> $date
		 );
		 $resultID = db_insert_data("gallerys", $data, $date);		 
		 return $resultID;
		 
		 /*
		 $wp_filetype = wp_check_filetype(basename($filename), null );
		  
		 $attachment = array(
		     'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $filename ), 
		     'post_mime_type' => $wp_filetype['type'],
		     'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
		     'post_content' => '',
		     'post_status' => 'inherit'
		  );
		  $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
		  //update_post_meta($post_id, $wpdb->prefix.$meta_key, $attach_id);
		  add_post_meta($post_id, $wpdb->prefix.$meta_key, $attach_id);
		  // you must first include the image.php file
		  // for the function wp_generate_attachment_metadata() to work		  
		  $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
		  wp_update_attachment_metadata( $attach_id, $attach_data );
	  	*/
}

function db_check_file_type_upload($_FILES,$total,$file_name)
{
	$flag = true;
	for($i=0;$i<$total;$i++)
	{
		$type		= $_FILES[$file_name]['type'][$i];
		if($type!="image/gif" && $type!="image/jpg" && $type!="image/jpeg" && $type!="image/png" && $type!="image/bmp" && $type!="image/tif")
			return false;
	}
	return $flag;
}

function db_check_size_upload($_FILES,$total,$file_name,$total_size)
{
	$flag = true;
	$size = 0;
	for($i=0;$i<$total;$i++)
	{
		$size += $_FILES[$file_name]['size'][$i];
	}
	if($size>$total_size)
		return false;
	else 	
		return $flag;
}

function db_upload_multiple_photo($_FILES,$file_name)
{
	$loixayra = '';
	
	$countfile = count($_FILES[$file_name]['name']); // dem so luong file
	if($countfile>10)
	{
		$loixayra .= '\n Maximum 10 photos';
	}
	if(!db_check_file_type_upload($_FILES,$countfile,$file_name))
	{
		$loixayra .= '\n Type file invalid, cho phép (jpg,jpeg,png,bmp,gif)';
	}	
	if(!db_check_size_upload($_FILES,$countfile,$file_name,10000000))
	{
		$loixayra .= '\n File size Maximum 10MB';
	}	
	
	$memfile_array = array();	
	if($loixayra=='')
	{
		
		//print_r($_FILES[$file_name]);
		for($i=0;$i<$countfile;$i++)
		{				
				$name		= $_FILES[$file_name]['name'][$i];
				$type		= $_FILES[$file_name]['type'][$i];
				$tmp_name	= $_FILES[$file_name]['tmp_name'][$i];
				$error		= $_FILES[$file_name]['error'][$i];
				$size		= $_FILES[$file_name]['size'][$i];
				
			$memfile = array(
			'name'		=> $name,
			'type'		=> $type,
			'tmp_name'	=> $tmp_name,
			'error'		=> $error,
			'size'		=> $size
			);
			$memfile_array[] = $memfile;		
		}	
		
		$step=1;
		foreach ($memfile_array as $value) 
		{
			//if($step==1) // upload anh minh hoa
				//db_upload_photo($post_id,$value,'thumbnail');
				
			$re = db_upload_photo($value);	
				
			$step++;
		}
		$loixayra = "Upload Photos Success!";
			
	}
	return $loixayra;	
}
 
## ket thuc cac ham upload hinh anh
/*
 * Cach su dung up load anh
 * $post_id = 1;

	$post_id = db_insert_post($_POST);
	if($post_id)
	{	
	 	$result_error = db_upload_multiple_photo($_FILES,'Photo');
	 	$postID = $post_id;
	 	require_once TEMPLATEPATH.'/mails/xtemplate-email-tham-gia-cuoc-thi-anh.php';
	 	require_once TEMPLATEPATH.'/mails/xtemplate-email-tham-gia-cuoc-thi-anh-admin.php';
	}
	 
 * 
 */
